package com.tedu07.lcl.controller;

import com.tedu07.lcl.entity.Depository;
import com.tedu07.lcl.entity.User;
import com.tedu07.lcl.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class UserController {

    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/user/checkLogin")
    public User checkLogin(HttpSession session){
        User user = (User)session.getAttribute("user");
        if (user != null){
            return user;
        }
        else
            return null;

    }

    @RequestMapping("/user/logout")
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }

    @RequestMapping("/login")
    public int login(User user, String rem, HttpSession session, HttpServletResponse response){
        User u = mapper.login(user.getUname());
        if (u != null){
            if (u.getPassword().equals(user.getPassword())){
                //存cookie
                if (rem !=null){    //rem="on"
                    Cookie cookie1 = new Cookie("username",user.getUname());
                    Cookie cookie2 = new Cookie("password",u.getPassword());
                    response.addCookie(cookie1);
                    response.addCookie(cookie2);
                }

                //存session
                session.setAttribute("user",u);
                return 1;   //登录成功
            }
            else {
                return 3;   //密码错误
            }
        }
        else {
            return 2;   //用户名不存在
        }
    }

    @RequestMapping("/reg")
    public int reg(User user, HttpServletRequest request){
        if ("".equals(user.getUname()) || "".equals(user.getPassword())){
            return 3;   //用户名或密码为空
        }
        if (mapper.check(user.getUname()) == 0){
            //填写用户表
            user.setRegTime(new Date());
            System.out.println(user);
            mapper.reg(user);

            //建立私人仓库
            int uid = mapper.selectUidByUname(user.getUname());     //获取uid
            Depository depository = new Depository();
            depository.setUid(uid);
            mapper.createDepository(uid);   //填写仓库表

            User u = mapper.selectDidByUid(uid);
            int did =u.getDid();;
            mapper.updateDidByUid(did,uid);     //修改用户对应的仓库id
            return 1;
        }
        else {
            return 2;    //用户名已存在
        }

    }

    //定义图片路径
    @Value("${dirPath}")
    private String dirPath;
    //查询个人信息
    @RequestMapping("/user/selectById")
    public User selectById(HttpSession session){
        User u = (User) session.getAttribute("user");
        return mapper.selectAllById(u.getUid());
    }

    //修改个人信息
    @RequestMapping("user/upload")
    public void uploadById(User user,  MultipartFile picFile) throws IOException {
        System.out.println("user = " + user + "picFile = " + picFile);
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;


        SimpleDateFormat format = new SimpleDateFormat("/yyyy/MM/dd/");

        String datePath = format.format(new Date());

        File dirFile = new File(dirPath+datePath);

        if (!dirFile.exists()){
            dirFile.mkdirs();
        }

        //头像实际路径
        String filePath = dirPath+datePath+fileName;

        picFile.transferTo(new File(filePath));

        //数据库头像路径
        user.setPicUrl("netDisk"+datePath+fileName);
        mapper.updateById(user);

    }

    //修改密码
    @RequestMapping("user/updatePassword")
    public String updatePassword(String oldPassword, String newPassword1 ,String newPassword2,HttpSession session){
        if (!newPassword1.equals(newPassword2)){
            return "请重新确认新密码!";
        }
        User user = (User)session.getAttribute("user");
        if (oldPassword.equals(mapper.selectAllById(user.getUid()).getPassword())){
            mapper.updatePassword(newPassword1,user.getUid());
            return "密码修改成功!";
        }
        else {
            return "原密码不正确!";
        }
    }
}
